layui.config({
    base: "static/layui/module/"
}).extend({
	dynamicCondition: 'dynamicCondition/dynamicCondition',
	opTable: 'opTable/opTable'
}).use(['form','layer','jquery','table','laydate','upload','element','dynamicCondition','laytpl','tableMerge','opTable'],function(){
	var form = layui.form,
		layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table=layui.table,
        upload = layui.upload,
        laydate =layui.laydate,
        element = layui.element,
        dynamicCondition=layui.dynamicCondition,
        laytpl=layui.laytpl,
        opTable=layui.opTable,
        tableMerge =layui.tableMerge;

	var tableIns = table.render({
        elem:'#table'
    	,url:'/manage/settlement_report/list'
	    ,method:'post'
        ,page: true //开启分页
	    ,limit: 10
	    ,limits: [10,15,18,20,30,40,50]
	    ,loading: true
		,height: 'full-30'
		// ,totalRow: true
        ,cols: [[ //表头
        	{type: 'numbers', title: '序号'},
        	{field: 'order_no', title: '结算单号', minWidth:'120'},
        	{field: 'settlement_type', title: '结算状态', minWidth:'120', templet: function (d){
				return d.settlement_type=='0'?'正常结算':'异常结算';
			}},
        	{field: 'sale_order_no', title: '销售订单号', minWidth:'120'},
        	{field: 'customer_code', title: '客户编码', minWidth:'120'},
        	{field: 'customer_name', title: '客户名称', minWidth:'120'},
        	{field: 'customer_order', title: '客户订单号', minWidth:'120'},
        	{field: 'related_order', title: '关联订单号', minWidth:'120'},
        	{field: 'person_name', title: '业务员', minWidth:'120'},
        	{field: 'create_time', title: '创建时间', minWidth:'120'},
			{field: 'cinvcode', title: '产品编码', minWidth:'100'},
			{field: 'cinvname', title: '产品名称', minWidth:'100'},
			{field: 'type', title: '类型', minWidth:'100'},
			{field: 'cbatch', title: '批号', minWidth:'100'},
			{field: 'qty', title: '订单数量', minWidth:'120'},
			{field: 'total_settlement_qty', title: '累计结算数量', minWidth:'120'},
			{field: 'total_settlement_amount', title: '累计结算金额', minWidth:'120'},
			{field: 'settlement_qty', title: '结算数量', minWidth:'120'},
			{field: 'settlement_amount', title: '结算金额', minWidth:'120'},
			{field: 'serial_number', title: '工序序列号', minWidth:'100'},
			// {field: 'product_process', title: '加工工序编码', minWidth:'120'},
			// {field: 'product_process_name', title: '加工工序名称', minWidth:'120'},
			{field: 'order_process', title: '订单工序编码', minWidth:'120'},
			{field: 'order_process_name', title: '订单工序名称', minWidth:'120'},
			{field: 'pricing_method', title: '计价方式', minWidth:'90'},
			{field: 'expect_hours', title: '标准工时', minWidth:'90'},
			{field: 'act_hours', title: '实际工时', minWidth:'90'},
			{field: 'ioritaxcost', title: '订单单价', minWidth:'110'},
			{field: 'settlementcost', title: '结算单价', minWidth:'110'},
			{field: 'amountinfo', title: '金额明细', minWidth:'300'}
        ]]
    	,text: {
    		none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
    	}
    	,toolbar:'#tableToolbar'
    	,defaultToolbar: ['filter']
    });
	
	var dataFields = [
		{field:"a.customer_code", title:"客户", edit:"select", templet:"#customer"}
		,{field:"a.customer_order", title:"客户订单号", edit:"text"}
		,{field:"a.order_no", title:"结算单号", edit:"text"}
		,{field:"a.sale_order_no", title:"销售单号", edit:"text"}
		,{field:"a.parent_order_no", title:"父级订单号", edit:"text"}
		,{field:"a.related_order", title:"关联订单号", edit:"text"}
		,{field:"a.person_name", title:"业务员", edit:"text"}
		,{field:"b.cinvcode", title:"产品编码", edit:"text"}
		,{field:"b.cinvname", title:"产品名称", edit:"text"}
		,{field:"b.type", title:"类型", edit:"text"}
		,{field:"b.cbatch", title:"批次", edit:"text"}
		,{field:"c.order_process", title:"订单工序编码", edit:"text"}
		,{field:"c.order_process_name", title:"订单工序名称", edit:"text"}
	];
	var top_dc = undefined;
	var dc = dynamicCondition.create({
    	fields:dataFields
	  	,tableId:"table" 
		,conditionTextId:"#findMsg"
		,type:"complex" 
	  	,requestDataType:"json"
	  	,counts:13
		,queryCallBack:function(requestData){
		}
		,afterOpen: function (_dc) {
			top_dc = _dc;
		}
	});
	
    table.on('toolbar(table)', function(obj){
		switch(obj.event){
			case 'find':
				dc.open();
			break;
			case 'download':
				layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
					var conditionArray = new Array();
					if (top_dc != undefined) {
						for (let i in top_dc.cacheCondition) {
							if (top_dc.cacheCondition[i].conditionValueLeftVal.value != ''
								|| top_dc.cacheCondition[i].conditionValueRightVal.value != '' || top_dc.cacheCondition[i].conditionValueVal.value != '') {
								var condition = {
									field: top_dc.cacheCondition[i].conditionFieldVal
									,title: top_dc.data[i].title
									,type: top_dc.cacheCondition[i].conditionOptionVal
									,right: top_dc.cacheCondition[i].conditionValueRightVal.value
									,left: top_dc.cacheCondition[i].conditionValueLeftVal.value
									,value: top_dc.cacheCondition[i].conditionValueVal.value
								}
								conditionArray.push(condition);
							}
						}
					}
					window.location.href = "/manage/settlement_report/download?condition=" + encodeURI(JSON.stringify(conditionArray));
					layer.close(index);
				});
				break;
		};
	});
})
